﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using PhoneApp1;
using System.IO.IsolatedStorage;
using PhoneApp1.GeocoderService;
using System.Collections.ObjectModel;

namespace TaxiTouchClient
{
    public partial class Client_destination : PhoneApplicationPage
    {
        private const string mapKey = @"Ai8yroXkpmcBvxqn1TXtjlQhy3FJcObwJpvgxzOEDfgcvaJlOP2CCdCh585njAjA";
        private GeocodeResult location;

        public Client_destination()
        {
            InitializeComponent();
        }

        private void btn_ValiderDestination(object sender, RoutedEventArgs e)
        {
            if (textBox_adresse.Text.Length == 0 || textBox_ville.Text.Length == 0)
            {
                MessageBox.Show("Veuillez remplir au moins l'adresse et la ville");
            }
            else
            {
                getCoordinates(textBox_adresse.Text + " " + textBox_cp.Text + " " + textBox_ville.Text);
            }
        }

        private void btn_Moins_Click(object sender, RoutedEventArgs e)
        {
            int nbPersonne = Int32.Parse(textBlock_nbPersonne.Text);

            if (nbPersonne > 1)
            {
                nbPersonne--;
                textBlock_nbPersonne.Text = nbPersonne.ToString();
            }
        }

        private void btn_Plus_Click(object sender, RoutedEventArgs e)
        {
            int nbPersonne = Int32.Parse(textBlock_nbPersonne.Text);
            nbPersonne++;
            textBlock_nbPersonne.Text = nbPersonne.ToString();
        }

        private void getCoordinates(String Adresse)
        {
            try
            {
                GeocodeServiceClient geocodeserviceclientadresse = new GeocodeServiceClient("BasicHttpBinding_IGeocodeService");

                GeocodeRequest request = new GeocodeRequest();

                request.Options = new PhoneApp1.GeocoderService.GeocodeOptions();

                request.Options.Filters = new ObservableCollection<PhoneApp1.GeocoderService.FilterBase>();
                var filter = new PhoneApp1.GeocoderService.ConfidenceFilter();
                filter.MinimumConfidence = PhoneApp1.GeocoderService.Confidence.High;
                request.Options.Filters.Add(filter);

                request.Credentials = new PhoneApp1.GeocoderService.Credentials() { ApplicationId = mapKey };

                geocodeserviceclientadresse.GeocodeCompleted += geocodeService_GeocodeCompleted;
                request.Query = Adresse;
                geocodeserviceclientadresse.GeocodeAsync(request);

            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        private void geocodeService_GeocodeCompleted(object sender, PhoneApp1.GeocoderService.GeocodeCompletedEventArgs e)
        {
            if (e.Result.Results.Count != 0)
            {
                location = e.Result.Results[0];

                // Stockage dans le phone
                IsolatedStorageSettings storage = IsolatedStorageSettings.ApplicationSettings;
                if (storage.Contains("clientDestination"))
                {
                    storage["clientDestination"] = location;
                    storage["clientDestinationNb"] = textBlock_nbPersonne.Text;
                }
                else
                {
                    storage.Add("clientDestination", location);
                    storage.Add("clientDestinationNb", textBlock_nbPersonne.Text);
                }
                storage.Save();

                NavigationService.Navigate(new Uri("/Pages/Client_accueil.xaml", UriKind.Relative));
            }
        }
    }
}